
STK.register('app.mod.mora', function($){
    return function(){
        var MORATEMPLATE = '<div class="container" node-type="mora" style="display: none;"><div class="panel">\
									<div class="panel-header"><span>猜拳</span><a class="panel-header-button">X</a><a class="panel-header-button">[]</a><a class="panel-header-button">-</a></div>\
									<div class="panel-content"><div class="mora"><p><span node-type="mora1c"></span></p><p><button node-type="mora1Btn">停！</button></p></div><div class="mora"><p><span node-type="mora2c"></span></p><p><button node-type="mora2Btn">停！</button></p></div></div>\
								</div></div>';
        var dom = $.builder(MORATEMPLATE), mora = dom.list['mora'][0], mora1Btn = dom.list['mora1Btn'][0], mora2Btn = dom.list['mora2Btn'][0], mora2cSpan = dom.list['mora2c'][0], mora1cSpan = dom.list['mora1c'][0];
        $.E('mainbox').appendChild(mora);
        mora.style.display = "";
        var L = ['石头', '剪子', '布'];
        var mora1Lock = false, mora2Lock = false, mora1c = 0, mora2c = 2;
        $.addEvent(mora1Btn, "click", function(){
            mora1Lock = true;
        });
        $.addEvent(mora2Btn, "click", function(){
            mora2Lock = true;
        });
        var timeout1 = setInterval(function(){
            if (mora1Lock) {
                clearInterval(timeout1);
                vs();
                return;
            }
            //Math.ceil(Math.random()*10)
            mora1c = (mora1c + 1) % 3;
            mora1cSpan.innerHTML = L[mora1c];
        }, 200);
        var timeout2 = setInterval(function(){
            if (mora2Lock) {
                clearInterval(timeout2);
                vs();
                return;
            }
            //Math.ceil(Math.random()*10)
            mora2c = (mora2c + 1) % 3;
            mora2cSpan.innerHTML = L[mora2c];
        }, 200);
        var color = function(e){
            e.style.color = "red";
        };
        var vs = function(){
            if (mora1Lock && mora2Lock) {
                if (mora1c == 0) {
                    if (mora2c == 2) {
                        color(mora2cSpan);
                    }
                    else 
                        if (mora2c == 1) {
                            color(mora1cSpan);
                        }
                }
                else 
                    if (mora1c == 1) {
                        if (mora2c == 0) {
                            color(mora2cSpan);
                        }
                        else 
                            if (mora2c == 2) {
                                color(mora1cSpan);
                            }
                    }
                    else {
                        if (mora2c == 1) {
                            color(mora2cSpan);
                        }
                        else 
                            if (mora2c == 0) {
                                color(mora1cSpan);
                            }
                    }
            }
        }
        $.gListener.register('logout', function(){
            mora.style.display = "none";
        });
    };
});